package linkedlist;

public class LinkedListR<E> {
    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return "Node{" +
                    "e=" + e +
                    '}';
        }
    }

    private Node head;
    private int size;

    public LinkedListR() {
        head = null;
        size = 0;
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 在链表的index （0-based）位置添加新的元素e
     *
     * @param index
     * @param e
     */
    public void add(int index, E e) {
        if (index < 0 || index > size) throw new IllegalArgumentException("Add failed.Illegal index");
        head = add(head, index, e);
        size++;
    }

    /**
     * 在链表 head 的index 位置添加元素 e
     *
     * @param node
     * @param index
     * @param e
     * @return
     */
    private Node add(Node node, int index, E e) {
        if (index == 0) {
            return new Node(e, node);
        }
        node.next = add(node.next, index - 1, e);
        return node;
    }

    public void removeElement(E e) {
        head = removeElement(head, e);
    }

    public void addFirst(E e) {
        add(0, e);
    }

    public void addLast(E e) {
        add(size - 1, e);
    }

    public E get(int index) {
        if (index < 0 || index >= size) throw new IllegalArgumentException("Get failed . Illegal index");
        return get(head, index).e;
    }

    private Node get(Node node, int index) {

        if (index == 0) return node;
        node = get(node.next, index - 1);
        return node;
    }

    public E getFirst(int index) {
        return get(0);
    }

    public E getLast(int index) {
        return get(size - 1);
    }

    /**
     * @param node
     * @param e
     * @return
     */
    private Node removeElement(Node node, E e) {
        if (node == null) return null;
        if (node.e.equals(e)) {
            size--;
            return node.next;
        }
        node.next = removeElement(node.next, e);
        return node;
    }

    public void set(int index, E e) {
        if (index < 0 || index >= size) throw new IllegalArgumentException("Set failed . Illegal index");
        set(head, index, e);
    }

    private void set(Node node, int index, E e) {
        if (index == 0) {
            node.e = e;
            return;
        }
        set(node.next, index - 1, e);
    }

    public boolean contains(E e) {
        return contains(head, e);
    }

    private boolean contains(Node node, E e) {
        if (node == null) return false;
        if (node.e.equals(e)) return true;
        return contains(node.next, e);
    }

    public E remove(int index) {
        if (index < 0 || index >= size) throw new IllegalArgumentException("remove failed . Illegal index");
        Node res = remove(head, index);
        size--;
        return res.e;
    }

    private Node remove(Node node, int index) {
        if (index == 0) {
            //跳过当前节点
            return node.next;
        }
        node.next = remove(node.next, index - 1);
        return node;

    }
}
